import React from "react";
import { Link, Routes, Route, useLocation } from "react-router-dom";

const Home = () => {
  return <div>这是首页</div>;
};
const About = () => {
  return <div>这是关于页</div>;
};

const Detail = () => {
  const location = useLocation();
  // console.log(location.search);

  const searchParams = new URLSearchParams(location.search);
  console.log(searchParams.get("id"));
  return <div>这是详情页</div>;
};

const App = () => {
  return (
    <>
      <h2>query动态传参</h2>
      <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          {/* <Link to="/detail?id=123&num=1">详情页</Link> */}
          <Link
            to={{
              pathname: "/detail",
              search: "?id=123&num=1",
              hash: "#nnn",
            }}
          >
            详情页
          </Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        {/* query的传参是不需要修改Route的写法 */}
        <Route path="/detail" element={<Detail />}></Route>
      </Routes>
    </>
  );
};

export default App;
